package edu.hawaii.senin.iclouds.plotting;

import java.util.TreeMap;

/**
 * Encapsulates surface population that is restored by parsing the log of the actual run.
 *
 * @author Pavel Senin.
 *
 */
public class GeneralPopulationStorage {

  private TreeMap<String, Long> populationStorage;
  private Long normaliser;

  /**
   * Instantiates this population.
   *
   * @param grainPopulation population to use.
   */
  public GeneralPopulationStorage(TreeMap<String, Long> grainPopulation) {
    this.populationStorage = grainPopulation;
    this.normaliser = 0L;
    for (Long value : this.populationStorage.values()) {
      this.normaliser += value;
    }
  }

  /**
   * Reports percentages of species.
   *
   * @param aim specie to calculate percentage fraction.
   * @return percentage.
   */
  public Double getPercentage(String aim) {
    Long acValue = this.populationStorage.get(aim);
    if (null == acValue) {
      return 0D;
    }
    else {
      return acValue.doubleValue() / this.normaliser;
    }
  }

  /**
   * Reports specie volume.
   *
   * @param specie specie of the interest.
   * @return the specie volume.
   */
  public Long getAmount(String specie) {
    return populationStorage.get(specie);
  }

  /**
   * Reports whether or not this population contains the specie.
   *
   * @param st specie to check for presence.
   * @return true if the specie is present, false otherwise.
   */
  public boolean contains(String st) {
    return this.populationStorage.keySet().contains(st);
  }

}
